package com.itheima.Service.CheckServicelmpl;

import com.itheima.Service.ReportService;
import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.ReportMapper;
import com.itheima.pojo.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReportServicelmpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    /**
     * 统计会员数量
     * @return
     */
    public Portdev list() {
        List<Integer> ARR=new ArrayList<>();
        //获取注册日期
        List<LocalDate> arr =reportMapper.listdate();
        for (LocalDate localDate : arr) {
            //根据日期统计数量
            Integer A=reportMapper.sumdate(localDate);
            ARR.add(A);
        }
        List<String> arr1 =new ArrayList<>();
        //日期格式转换
        for (LocalDate localDate : arr) {
            String s = localDate.toString();
            StringBuilder stringBuilder = new StringBuilder(s);
            stringBuilder.setCharAt(4,'.');
            stringBuilder.setCharAt(7,'.');
            arr1.add(stringBuilder.toString());
        }
        //String join1 = StringUtil.join(",", ARR);//数量
        //String join = StringUtil.join(",", arr1);//时间
        Portdev portdev = new Portdev();
        portdev.setMonths(arr1);
        portdev.setMemberCount(ARR);
        return portdev;

    }

    /**
     * 统计套餐数据
     * @return
     */

    public Protdecsetmeal setlist() {
        //获取套餐名字
        List<String> setmealname =reportMapper.listsetmeal();
        //统计这个套餐有多少人预约了
        List<Protsetmealdo> ARR=reportMapper.sun();
        //String join = StringUtil.join("", setmealname);
        //String join1 = StringUtil.join("", ARR);
        Protdecsetmeal p = new Protdecsetmeal();
        p.setSetmealNames(setmealname);
        p.setSetmealCount(ARR);
        return p;
    }

    /**
     * 获取运营数据
     * @return
     */
    public BusinessReportData read() {
        BusinessReportData businessReportData = new BusinessReportData();
        LocalDate now = LocalDate.now();
        businessReportData.setReportDate(now);//获取今天日期
        //获取今日预约数
        Integer datenow = reportMapper.datenow(now);
        businessReportData.setTodayOrderNumber(datenow);
        //获取本月的第一天
        LocalDate firstDayOfThisMonth = now.withDayOfMonth(1);
        //获取本月的最后一天
        LocalDate lastDayOfThisMonth = now.withDayOfMonth(now.lengthOfMonth());
        //获取本月的预约数
        Integer thisMonthOrderNumber = reportMapper.thisMonthOrderNumber(firstDayOfThisMonth, lastDayOfThisMonth);
        businessReportData.setThisMonthOrderNumber(thisMonthOrderNumber);
        //获取当前日期的本周第一天
        Date thisWeekMonday = DateUtils.getThisWeekMonday();
        //获取当前日期的本周最后一天
        Date thisWeekSunday = DateUtils.getSundayOfThisWeek();
        Integer thisWeeOrderMember = reportMapper.thisWeekNewMember(thisWeekMonday, thisWeekSunday);
        businessReportData.setThisWeekOrderNumber(thisWeeOrderMember);
        //查询会员数
        Integer todayNewMember=reportMapper.insertmumber(now);//今日新增会员数
        Integer totalMember=reportMapper.mumber();//总会员数
        Integer thisMonthNewMember=reportMapper.mouthmumber(firstDayOfThisMonth,lastDayOfThisMonth);//本月
        Integer thisWeekNewMember=reportMapper.weekmumber(thisWeekMonday,thisWeekSunday);//本周
        businessReportData.setTodayNewMember(todayNewMember);
        businessReportData.setTotalMember(totalMember);
        businessReportData.setThisMonthNewMember(thisMonthNewMember);
        businessReportData.setThisWeekNewMember(thisWeekNewMember);

        //查询到诊断数
        Integer todayVisitsNumber =reportMapper.todayVisitsNumber(now);
        Integer thisMonthVisitsNumber =reportMapper.thisMonthVisitsNumber(firstDayOfThisMonth,lastDayOfThisMonth);
        Integer thisWeekVisitsNumber =reportMapper.thisWeekVisitsNumber(thisWeekMonday,thisWeekSunday);
        businessReportData.setTodayVisitsNumber(todayVisitsNumber);
        businessReportData.setThisMonthVisitsNumber(thisMonthVisitsNumber);
        businessReportData.setThisWeekVisitsNumber(thisWeekVisitsNumber);
        //套餐集合
        Integer sumnorder=reportMapper.ordersert();//查询总预约数
        List<HotSetmeal> ARR=reportMapper.hotSetmeal();
        for (HotSetmeal hotSetmeal : ARR) {
            //计算套餐占比
            Integer setmealcount = hotSetmeal.getSetmealcount();
            Double A = setmealcount * 1.0 / sumnorder;
            hotSetmeal.setProportion(A);
        }
        businessReportData.setHotSetmeal(ARR);
        return businessReportData;
    }

    /**
     * 模板导出
     * @param response
     */
    public void exclelist(HttpServletResponse response) {
        LocalDate now = LocalDate.now();//获取当前日期
        String s = now.toString();
        String formattedDate = now.format(DateTimeFormatter.ofPattern("yyyy.MM.dd")); // 格式化日期
        BusinessReportData read = read();
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("templates/report_template.xlsx");//获取目录下的模板
        try {
            XSSFWorkbook xssfWorkbook=new XSSFWorkbook(in);
            XSSFSheet sheet1 = xssfWorkbook.getSheet("Sheet1");
            sheet1.getRow(2).getCell(5).setCellValue(s);
            sheet1.getRow(4).getCell(5).setCellValue(read.getTodayNewMember());
            sheet1.getRow(4).getCell(7).setCellValue(read.getTotalMember());
            sheet1.getRow(5).getCell(5).setCellValue(read.getThisWeekNewMember());
            sheet1.getRow(5).getCell(7).setCellValue(read.getThisMonthNewMember());
            sheet1.getRow(7).getCell(5).setCellValue(read.getTodayOrderNumber());
            sheet1.getRow(7).getCell(7).setCellValue(read.getTodayVisitsNumber());
            sheet1.getRow(8).getCell(5).setCellValue(read.getThisWeekOrderNumber());
            sheet1.getRow(8).getCell(7).setCellValue(read.getThisWeekVisitsNumber());
            sheet1.getRow(9).getCell(5).setCellValue(read.getThisMonthOrderNumber());
            sheet1.getRow(9).getCell(7).setCellValue(read.getThisMonthVisitsNumber());
            List<HotSetmeal> hotSetmeal = read.getHotSetmeal();
            // 使用泛型方法将 List 转换为 HotSetmeal 数组
            HotSetmeal[] setmealsArray = hotSetmeal.toArray(new HotSetmeal[0]);
            // 遍历数组并填充数据
            for (int i = 0; i < setmealsArray.length; i++) {
                HotSetmeal setmeal = setmealsArray[i];
                sheet1.getRow(12 + i).getCell(4).setCellValue(setmeal.getName());
                sheet1.getRow(12 + i).getCell(5).setCellValue(setmeal.getSetmealcount());
                sheet1.getRow(12 + i).getCell(6).setCellValue(setmeal.getProportion());
            }
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=BusinessReport_" + formattedDate + ".xlsx");
            ServletOutputStream outputStream = response.getOutputStream();
                xssfWorkbook.write(outputStream);
                outputStream.close();
                xssfWorkbook.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to export Excel file.", e);
        }
    }
}
